package com.sena.controller;

import com.sena.base.BaseDateMessage;
import com.sena.base.BaseMessage;
import com.sena.domain.SenaBlackRegexConfig;
import com.sena.domain.SenaClientSite;
import com.sena.domain.SenaSuffixConfig;
import com.sena.domain.SenaWhiteRegexConfig;
import com.sena.enums.ResultCodeEnum;
import com.sena.exception.ThreadRunTimeException;
import com.sena.service.SenaClientSiteService;
import com.sena.vo.RequestBlackList;
import com.sena.vo.RequestSiteList;
import com.sena.vo.RequestSuffixList;
import com.sena.vo.RequestWhiteList;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @author Admin
 */
@Slf4j
@RequestMapping("/site")
@Controller
public class SenaClientSiteController {


    @Autowired
    private SenaClientSiteService senaClientSiteService;

    @PostMapping("/listByPage")
    @ResponseBody
    public BaseDateMessage listByPage(@RequestBody RequestSiteList request) {

        BaseDateMessage baseDateMessage = new BaseDateMessage();
        try {
            baseDateMessage.setData(senaClientSiteService.listByPage(request));
            baseDateMessage.buildSuccess(ResultCodeEnum.SUCCESS);
            baseDateMessage.setMsg("获取成功");
        } catch (ThreadRunTimeException exception) {
            baseDateMessage.buildSuccess(exception.getResultCodeEnum());
        } catch (Exception e) {
            log.info(e.getMessage());
            baseDateMessage.buildFail(ResultCodeEnum.FAIL);
            baseDateMessage.setMsg("获取失败");
        }
        return baseDateMessage;
    }


    @PostMapping("/add")
    @ResponseBody
    public BaseDateMessage add(@RequestBody SenaClientSite site) {

        BaseDateMessage baseDateMessage = new BaseDateMessage();
        try {
            int count = senaClientSiteService.addClientSite(site);
            baseDateMessage.buildSuccess(ResultCodeEnum.SUCCESS);
            baseDateMessage.setMsg("添加成功");
        } catch (ThreadRunTimeException exception) {
            baseDateMessage.buildSuccess(exception.getResultCodeEnum());
        } catch (Exception e) {
            log.info(e.getMessage());
            baseDateMessage.buildFail(ResultCodeEnum.FAIL);
            baseDateMessage.setMsg("添加失败");
        }
        return baseDateMessage;
    }

    @PostMapping("/update")
    @ResponseBody
    public BaseDateMessage update(@RequestBody SenaClientSite site) {

        BaseDateMessage baseDateMessage = new BaseDateMessage();
        try {
            baseDateMessage.setData(senaClientSiteService.updateClientSite(site));
            baseDateMessage.buildSuccess(ResultCodeEnum.SUCCESS);
            baseDateMessage.setMsg("获取成功");
        } catch (ThreadRunTimeException exception) {
            baseDateMessage.buildSuccess(exception.getResultCodeEnum());
        } catch (Exception e) {
            log.info(e.getMessage());
            baseDateMessage.buildFail(ResultCodeEnum.FAIL);
            baseDateMessage.setMsg("获取失败");
        }
        return baseDateMessage;
    }

    @GetMapping("/delete")
    @ResponseBody
    public BaseDateMessage delete(@RequestParam(value = "id") int id) {
        BaseDateMessage baseDateMessage = new BaseDateMessage();
        try {
            baseDateMessage.setData(senaClientSiteService.deleteClientSite(id));
            baseDateMessage.buildSuccess(ResultCodeEnum.SUCCESS);
            baseDateMessage.setMsg("获取成功");
        } catch (ThreadRunTimeException exception) {
            baseDateMessage.buildSuccess(exception.getResultCodeEnum());
        } catch (Exception e) {
            log.info(e.getMessage());
            baseDateMessage.buildFail(ResultCodeEnum.FAIL);
            baseDateMessage.setMsg("获取失败");
        }
        return baseDateMessage;
    }


    @GetMapping("/select")
    @ResponseBody
    public BaseDateMessage select(@RequestParam(value = "id") int id) {
        BaseDateMessage baseDateMessage = new BaseDateMessage();
        try {
            baseDateMessage.setData(senaClientSiteService.selectByPrimaryKey(id));
            baseDateMessage.buildSuccess(ResultCodeEnum.SUCCESS);
            baseDateMessage.setMsg("获取成功");
        } catch (ThreadRunTimeException exception) {
            baseDateMessage.buildSuccess(exception.getResultCodeEnum());
        } catch (Exception e) {
            log.info(e.getMessage());
            baseDateMessage.buildFail(ResultCodeEnum.FAIL);
            baseDateMessage.setMsg("获取失败");
        }
        return baseDateMessage;
    }

    /**
     * 添加
     * @param blackRegexConfig
     * @return
     */
    @PostMapping("/addblack")
    @ResponseBody
    public BaseMessage addblack(@RequestBody SenaBlackRegexConfig blackRegexConfig) {
        BaseMessage msg = new BaseMessage();
        try {
            senaClientSiteService.addSenaBlackRegexConfig(blackRegexConfig);
            msg.buildSuccess(ResultCodeEnum.SUCCESS);
            msg.setMsg("操作成功");
        } catch (ThreadRunTimeException exception) {
            msg.buildSuccess(exception.getResultCodeEnum());
        } catch (Exception e) {
            log.info(e.getMessage());
            msg.setCode("500");
            msg.setMsg("操作失败");
        }
        return msg;
    }


    /**
     * 添加
     * @param whiteRegexConfig
     * @return
     */
    @PostMapping("/addwhite")
    @ResponseBody
    public BaseMessage addwhite(@RequestBody SenaWhiteRegexConfig whiteRegexConfig) {
        BaseMessage msg = new BaseMessage();
        try {
            senaClientSiteService.addSenaWhiteRegexConfig(whiteRegexConfig);
            msg.buildSuccess(ResultCodeEnum.SUCCESS);
            msg.setMsg("添加成功");
        } catch (ThreadRunTimeException exception) {
            msg.buildSuccess(exception.getResultCodeEnum());
        } catch (Exception e) {
            log.info(e.getMessage());
            msg.setCode("500");
            msg.setMsg("添加失败");
        }
        return msg;
    }


    @PostMapping("/addsuffix")
    @ResponseBody
    public BaseMessage addsuffix(@RequestBody SenaSuffixConfig senaSuffixConfig) {
        BaseMessage msg = new BaseMessage();
        try {
            senaClientSiteService.addSenaSuffixConfig(senaSuffixConfig);
            msg.buildSuccess(ResultCodeEnum.SUCCESS);
            msg.setMsg("添加成功");
        } catch (ThreadRunTimeException exception) {
            msg.buildSuccess(exception.getResultCodeEnum());
        } catch (Exception e) {
            log.info(e.getMessage());
            msg.setCode("500");
            msg.setMsg("添加失败");
        }
        return msg;
    }





    @PostMapping("/listByPageBlack")
    @ResponseBody
    public BaseDateMessage listByPageBlack(@RequestBody RequestBlackList request) {

        BaseDateMessage baseDateMessage = new BaseDateMessage();
        try {
            baseDateMessage.setData(senaClientSiteService.listByPageBlack(request));
            baseDateMessage.buildSuccess(ResultCodeEnum.SUCCESS);
            baseDateMessage.setMsg("获取成功");
        } catch (ThreadRunTimeException exception) {
            baseDateMessage.buildSuccess(exception.getResultCodeEnum());
        } catch (Exception e) {
            log.info(e.getMessage());
            baseDateMessage.buildFail(ResultCodeEnum.FAIL);
            baseDateMessage.setMsg("获取失败");
        }
        return baseDateMessage;
    }


    @PostMapping("/listByPageWhite")
    @ResponseBody
    public BaseDateMessage listByPageWhite(@RequestBody RequestWhiteList request) {

        BaseDateMessage baseDateMessage = new BaseDateMessage();
        try {
            baseDateMessage.setData(senaClientSiteService.listByPageWhite(request));
            baseDateMessage.buildSuccess(ResultCodeEnum.SUCCESS);
            baseDateMessage.setMsg("获取成功");
        } catch (ThreadRunTimeException exception) {
            baseDateMessage.buildSuccess(exception.getResultCodeEnum());
        } catch (Exception e) {
            log.info(e.getMessage());
            baseDateMessage.buildFail(ResultCodeEnum.FAIL);
            baseDateMessage.setMsg("获取失败");
        }
        return baseDateMessage;
    }
    @PostMapping("/listByPageSuffix")
    @ResponseBody
    public BaseDateMessage listByPageSuffix(@RequestBody RequestSuffixList request) {

        BaseDateMessage baseDateMessage = new BaseDateMessage();
        try {
            baseDateMessage.setData(senaClientSiteService.listByPageSuffix(request));
            baseDateMessage.buildSuccess(ResultCodeEnum.SUCCESS);
            baseDateMessage.setMsg("获取成功");
        } catch (ThreadRunTimeException exception) {
            baseDateMessage.buildSuccess(exception.getResultCodeEnum());
        } catch (Exception e) {
            log.info(e.getMessage());
            baseDateMessage.buildFail(ResultCodeEnum.FAIL);
            baseDateMessage.setMsg("获取失败");
        }
        return baseDateMessage;
    }


    @GetMapping("/deletesuffix")
    @ResponseBody
    public BaseDateMessage desffix(@RequestParam(value = "id") int id) {
        BaseDateMessage baseDateMessage = new BaseDateMessage();
        try {
            baseDateMessage.setData(senaClientSiteService.deleteSuffix(id));
            baseDateMessage.buildSuccess(ResultCodeEnum.SUCCESS);
            baseDateMessage.setMsg("获取成功");
        } catch (ThreadRunTimeException exception) {
            baseDateMessage.buildSuccess(exception.getResultCodeEnum());
        } catch (Exception e) {
            log.info(e.getMessage());
            baseDateMessage.buildFail(ResultCodeEnum.FAIL);
            baseDateMessage.setMsg("获取失败");
        }
        return baseDateMessage;
    }
    @GetMapping("/deleteblack")
    @ResponseBody
    public BaseDateMessage delblack(@RequestParam(value = "id") int id) {
        BaseDateMessage baseDateMessage = new BaseDateMessage();
        try {
            baseDateMessage.setData(senaClientSiteService.deleteBlack(id));
            baseDateMessage.buildSuccess(ResultCodeEnum.SUCCESS);
            baseDateMessage.setMsg("获取成功");
        } catch (ThreadRunTimeException exception) {
            baseDateMessage.buildSuccess(exception.getResultCodeEnum());
        } catch (Exception e) {
            log.info(e.getMessage());
            baseDateMessage.buildFail(ResultCodeEnum.FAIL);
            baseDateMessage.setMsg("获取失败");
        }
        return baseDateMessage;
    }
    @GetMapping("/deletewhite")
    @ResponseBody
    public BaseDateMessage delwhite(@RequestParam(value = "id") int id) {
        BaseDateMessage baseDateMessage = new BaseDateMessage();
        try {
            baseDateMessage.setData(senaClientSiteService.deleteWhite(id));
            baseDateMessage.buildSuccess(ResultCodeEnum.SUCCESS);
            baseDateMessage.setMsg("获取成功");
        } catch (ThreadRunTimeException exception) {
            baseDateMessage.buildSuccess(exception.getResultCodeEnum());
        } catch (Exception e) {
            log.info(e.getMessage());
            baseDateMessage.buildFail(ResultCodeEnum.FAIL);
            baseDateMessage.setMsg("获取失败");
        }
        return baseDateMessage;
    }


    @PostMapping("/updateblack")
    @ResponseBody
    public BaseMessage updateblack(@RequestBody SenaBlackRegexConfig blackRegexConfig) {
        BaseMessage msg = new BaseMessage();
        try {
            senaClientSiteService.updateSenaBlackRegexConfig(blackRegexConfig);
            msg.buildSuccess(ResultCodeEnum.SUCCESS);
            msg.setMsg("操作成功");
        } catch (ThreadRunTimeException exception) {
            msg.buildSuccess(exception.getResultCodeEnum());
        } catch (Exception e) {
            log.info(e.getMessage());
            msg.setCode("500");
            msg.setMsg("操作失败");
        }
        return msg;
    }


    /**
     * 添加
     * @param whiteRegexConfig
     * @return
     */
    @PostMapping("/updatewhite")
    @ResponseBody
    public BaseMessage updatewhite(@RequestBody SenaWhiteRegexConfig whiteRegexConfig) {
        BaseMessage msg = new BaseMessage();
        try {
            senaClientSiteService.updateSenaWhiteRegexConfig(whiteRegexConfig);
            msg.buildSuccess(ResultCodeEnum.SUCCESS);
            msg.setMsg("操作成功");
        } catch (ThreadRunTimeException exception) {
            msg.buildSuccess(exception.getResultCodeEnum());
        } catch (Exception e) {
            log.info(e.getMessage());
            msg.setCode("500");
            msg.setMsg("操作失败");
        }
        return msg;
    }


    @PostMapping("/updatesuffix")
    @ResponseBody
    public BaseMessage updatesuffix(@RequestBody SenaSuffixConfig senaSuffixConfig) {
        BaseMessage msg = new BaseMessage();
        try {
            senaClientSiteService.updateSenaSuffixConfig(senaSuffixConfig);
            msg.buildSuccess(ResultCodeEnum.SUCCESS);
            msg.setMsg("操作成功");
        } catch (ThreadRunTimeException exception) {
            msg.buildSuccess(exception.getResultCodeEnum());
        } catch (Exception e) {
            log.info(e.getMessage());
            msg.setCode("500");
            msg.setMsg("操作失败");
        }
        return msg;
    }



    @GetMapping("/selectblack")
    @ResponseBody
    public BaseDateMessage selectblack(@RequestParam(value = "id") int id) {
        BaseDateMessage baseDateMessage = new BaseDateMessage();
        try {
            baseDateMessage.setData(senaClientSiteService.selectBlack(id));
            baseDateMessage.buildSuccess(ResultCodeEnum.SUCCESS);
            baseDateMessage.setMsg("获取成功");
        } catch (ThreadRunTimeException exception) {
            baseDateMessage.buildSuccess(exception.getResultCodeEnum());
        } catch (Exception e) {
            log.info(e.getMessage());
            baseDateMessage.buildFail(ResultCodeEnum.FAIL);
            baseDateMessage.setMsg("获取失败");
        }
        return baseDateMessage;
    }
    @GetMapping("/selectwhite")
    @ResponseBody
    public BaseDateMessage selectwhite(@RequestParam(value = "id") int id) {
        BaseDateMessage baseDateMessage = new BaseDateMessage();
        try {
            baseDateMessage.setData(senaClientSiteService.selectWhite(id));
            baseDateMessage.buildSuccess(ResultCodeEnum.SUCCESS);
            baseDateMessage.setMsg("获取成功");
        } catch (ThreadRunTimeException exception) {
            baseDateMessage.buildSuccess(exception.getResultCodeEnum());
        } catch (Exception e) {
            log.info(e.getMessage());
            baseDateMessage.buildFail(ResultCodeEnum.FAIL);
            baseDateMessage.setMsg("获取失败");
        }
        return baseDateMessage;
    }

    @GetMapping("/selectsuffix")
    @ResponseBody
    public BaseDateMessage selectsuffix(@RequestParam(value = "id") int id) {
        BaseDateMessage baseDateMessage = new BaseDateMessage();
        try {
            baseDateMessage.setData(senaClientSiteService.selectSuffix(id));
            baseDateMessage.buildSuccess(ResultCodeEnum.SUCCESS);
            baseDateMessage.setMsg("获取成功");
        } catch (ThreadRunTimeException exception) {
            baseDateMessage.buildSuccess(exception.getResultCodeEnum());
        } catch (Exception e) {
            log.info(e.getMessage());
            baseDateMessage.buildFail(ResultCodeEnum.FAIL);
            baseDateMessage.setMsg("获取失败");
        }
        return baseDateMessage;
    }


    @GetMapping("/start")
    @ResponseBody
    public BaseDateMessage start() {
        BaseDateMessage baseDateMessage = new BaseDateMessage();
        try {
            senaClientSiteService.start();
            baseDateMessage.buildSuccess(ResultCodeEnum.SUCCESS);
            baseDateMessage.setMsg("操作成功");
        } catch (ThreadRunTimeException exception) {
            baseDateMessage.buildSuccess(exception.getResultCodeEnum());
        } catch (Exception e) {
            log.info(e.getMessage());
            baseDateMessage.buildFail(ResultCodeEnum.FAIL);
            baseDateMessage.setMsg("操作成功");
        }
        return baseDateMessage;
    }


}
